﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows;
using DataStore;
using Infrastructure.ViewModels;
using Microsoft.Windows.Controls;
using NHibernate;

namespace UI
{
	/// <summary>
	/// Interaction logic for Category.xaml
	/// </summary>
	public partial class Category : UserControl
	{
        /// <summary>
        /// Occurs when [selection changed].
        /// </summary>
        public event EventHandler<DataGridRowEditEndingEventArgs> SelectionChanged;

        /// <summary>
        /// Called when [selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs"/> instance containing the event data.</param>
        public void OnSelectionChanged(object sender, DataGridRowEditEndingEventArgs e)
        {
            if(SelectionChanged != null)
            {
                SelectionChanged(sender, e);
            }
        }

        AllCategoriesViewModel _viewModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="Category"/> class.
        /// </summary>
	    public Category()
		{
		    this.InitializeComponent();

		    LoadCategoryData();

            dataGrid1.RowEditEnding += new EventHandler<Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs>(dataGrid1_RowEditEnding);
		}

        /// <summary>
        /// Handles the RowEditEnding event of the dataGrid1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs"/> instance containing the event data.</param>
        void dataGrid1_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
        {
            if(e.EditAction == DataGridEditAction.Commit)
            {
                OnSelectionChanged(sender, e);
            }
        }

        /// <summary>
        /// Loads the category data.
        /// Sets selected item's checkbox
        /// </summary>
	    void LoadCategoryData()
	    {
            if (!DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow))
            {
                _viewModel = new AllCategoriesViewModel();
                _viewModel.Loaded += new EventHandler<EventArgs>(delegate
                                                                     {
                                                                         dataGrid1.ItemsSource = _viewModel.Categories;
                                                                         UpdateSelectedItems();
                                                                     });
            }
	    }

        /// <summary>
        /// Set selected item's checkbox.
        /// </summary>
	    void UpdateSelectedItems()
	    {
            if(_selected == null)
            {
                return;
            }

            if(!NHibernateUtil.IsInitialized(_selected))
            {
                NHibernateUtil.Initialize(_selected);
            }

            foreach (CategoryViewModel item in dataGrid1.Items)
            {
                foreach (DataStore.Domain.Category selected in _selected)
                {
                    if(item.Id == selected.Id)
                    {
                        item.Selected = true;
                    }
                }
            }
	    }

        /// <summary>
        /// Gets the viewmodel of selected categories.
        /// </summary>
        /// <value>The selected categories VM.</value>
	    public IList SelectedCategoriesVM 
        { 
            get
            {
                return dataGrid1.SelectedItems;
            }
	    }

	    ICollection<DataStore.Domain.Category> _selected = new List<DataStore.Domain.Category>();
        /// <summary>
        /// Gets or sets the selected categories.
        /// </summary>
        /// <value>The selected categories.</value>
        public ICollection<DataStore.Domain.Category> SelectedCategories
        {
            get
            {
                List<DataStore.Domain.Category> categories = new List<DataStore.Domain.Category>();
                foreach (CategoryViewModel item in dataGrid1.Items)
                {
                    if (item.Selected)
                    {
                        categories.Add(item.Category);
                    }
                }
                return categories;
            }
            set
            {
                _selected = value;
                UpdateSelectedItems();
            }
        }
	}
}